﻿@page "/playlists/create"
@using MusicX.Web.Shared.Playlists

<ErrorsList Errors="response?.Errors" />
<ErrorsList Errors="createResponse?.Errors" />

<h1>Add "now playing" songs to a playlist</h1>

<div class="row">
    <div class="col-sm-9 col-md-7 col-lg-5 my-3">
        <EditForm Model="model" OnValidSubmit="Submit">
            <DataAnnotationsValidator />
            <ValidationSummary />
            @if (this.response?.Data != null)
            {
                <div class="input-group mb-3">
                    <div class="input-group-prepend w-25">
                        <span class="input-group-text w-100">Playlist</span>
                    </div>
                    <InputSelectNumber class="form-control" @bind-Value="model.Id">
                        <option value="0">(+) Create a new playlist</option>
                        @foreach (var playlist in this.response?.Data?.Playlists)
                        {
                            <option value=@playlist.Id>@playlist.Name</option>
                        }
                    </InputSelectNumber>
                </div>
            }
            @if (this.model.Id == 0)
            {
                <div class="input-group mb-3">
                    <div class="input-group-prepend w-25">
                        <span class="input-group-text w-100">Playlist name</span>
                    </div>
                    <InputText class="form-control" @bind-Value="model.Name" required />
                </div>
            }
            <button class="btn btn-success btn-lg" type="submit">Save</button>
        </EditForm>
    </div>
</div>

@code {
    ApiResponse<GetAllPlaylistResponse> response;

    ApiResponse<CreatePlaylistFromListResponse> createResponse;

    CreatePlaylistFromListRequest model = new CreatePlaylistFromListRequest();

    protected override async Task OnInitializedAsync()
    {
        this.response = await this.ApiClient.GetAllPlaylists();
    }

    async Task Submit()
    {
        model.SongIds = this.MediaPlayer.Playlist.Select(x => x.Id);
        this.createResponse = await this.ApiClient.CreatePlaylistFromList(model);
        if (this.createResponse.IsOk)
        {
            this.NavigationManager.NavigateTo("playlists");
        }
    }
}
